package redis.datatype.impl;

import java.util.Set;

import redis.Constant;
import redis.Constant.Sort;
import redis.clients.jedis.ShardedJedis;
import redis.datatype.ISortedSets;

public class ISortedSetsImpl implements ISortedSets {

    public long set(ShardedJedis jedis, String key, double score, String value) {
        return jedis.zadd(key, score, value);

    }

    public long count(ShardedJedis jedis, String key) {
        return jedis.zcard(key);
    }

    public long countInScore(ShardedJedis jedis, String key, double scoreStart,
            double scoreEnd) {
        return jedis.zcount(key, scoreStart, scoreEnd);
    }

    public Set loadALL(ShardedJedis jedis, String key, Constant.Sort sort) {
        Set set = null;
        switch (sort) {
        case DESC:
            set = jedis.zrevrange(key, 0, -1);
            break;
        case ASC:
            set = jedis.zrange(key, 0, -1);
            break;
        }
        return set;
    }

    public Set loadByPage(ShardedJedis jedis, String key, int start, int end,
            Constant.Sort sort) {
        Set set = null;
        switch (sort) {
        case DESC:
            set = jedis.zrevrange(key, start, end);
            break;
        case ASC:
            set = jedis.zrange(key, start, end);
            break;
        }
        return set;
    }

    public double getScore(ShardedJedis jedis, String key, String value) {
        return jedis.zscore(key, value);
    }

    public long delete(ShardedJedis jedis, String key, String value) {
        return jedis.zrem(key, value);
    }

    public long delete(ShardedJedis jedis, String key) {
        return jedis.del(key);
    }
}
